package com.nefu.editor.service.Impl;

import com.nefu.editor.common.EdiException;
import com.nefu.editor.core.mapper.ArticleMapper;
import com.nefu.editor.core.mapper.CollectsMapper;
import com.nefu.editor.core.mapper.UserMapper;
import com.nefu.editor.core.model.Article;
import com.nefu.editor.core.model.Collects;
import com.nefu.editor.core.model.User;
import com.nefu.editor.service.CollectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenchenT
 * @date : 18-11-13
 * @since : java 8
 */
@Service
public class CollectsServiceImpl implements CollectsService {

    private final CollectsMapper collectsMapper;
    private final ArticleMapper articleMapper;
    private final UserMapper userMapper;

    @Autowired
    public CollectsServiceImpl(CollectsMapper collectsMapper, ArticleMapper articleMapper, UserMapper userMapper) {
        this.collectsMapper = collectsMapper;
        this.articleMapper = articleMapper;
        this.userMapper = userMapper;
    }

    @Override
    public List<Map<String, Object>> getArticlesByUserSystemId(User user) throws EdiException {
        List<Map<String, Object>> rtv = new ArrayList<>();
        if (null != user) {
            List<Collects> collects = collectsMapper.selectCollectsByUserSystemId(user);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (null != collects && 0 != collects.size()) {
                for (Collects collect : collects) {
                    Map<String, Object> map = new HashMap<>(8);
                    Article article = new Article();
                    article.setSystemId(collect.getArticleSystemId());
                    List<Article> articles = articleMapper.selectArticleByCondition(article);
                    if (null != articles && 1 == articles.size()) {
                        map.put("systemId", articles.get(0).getSystemId());
                        map.put("title", articles.get(0).getTitle());
                        map.put("time", formatter.format(collect.getTime()));
                        map.put("summary", articles.get(0).getSummary());
                        map.put("image", articles.get(0).getImage());
                        map.put("likesNumber", articles.get(0).getLikesNumber());
                        map.put("collectsNumber", articles.get(0).getCollectsNumber());
                        User newUser = new User();
                        newUser.setSystemId(articles.get(0).getWriterId());
                        List<User> users = userMapper.selectUserByCondition(newUser);
                        if (null != users && 1 == users.size()) {
                            map.put("writer", user.getIdNumber());
                        }
                    }
                    rtv.add(map);
                }
            } else {
                throw new EdiException("收藏列表为空!");
            }
        } else {
            throw new EdiException("参数错误!");
        }
        return rtv;
    }
}
